const cloud = require('wx-server-sdk')
cloud.init({
    env: 'zbdc-6gavbhj633c62e27'
})
const db = cloud.database();
const _ = db.command

exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()
    if (event.type == 'addmaster'){ // 已掌握词本
        return await db.collection('masterList').add({ 
            data:{
              openid:event.openid,
              year: event.year,
              mon: event.month,
              day: event.day,
              newArr: event.origin_data //对象数组
            }
          })
    }else if (event.type == 'addstar'){ // 收藏
        await db.collection('userData') 
        .where({openid:wxContext.OPENID})
        .update({
            data: {
              starData: _.unshift(event.word),
              alterTime:db.serverDate()
            },
            success: function(res) {
              console.log(res.data)
            }
        })
    }else if (event.type == 'addnote'){  // 生词本
        console.log(event.word)
        await db.collection('userData')
        .where({openid:wxContext.OPENID})
        .update({
            data: {
              noteBook: _.unshift(event.word),
              alterTime:db.serverDate()
            }
        })
    }else if (event.type == 'newUser'){
        console.log(event.openid,event.userName)
        await db.collection('userID').add({
            data: {
                openid: event.openid,
            }
        })
        await db.collection('userData').add({
            data: {
                openid:wxContext.OPENID,
                cet6Progress: 0, // 学习进度
                cet4Progress: 0,
                kaoyanProgress: 0,
                auth: 0, // 判断使用者身份
                alterTime: db.serverDate(), // 创建时间
                userName: event.userName, // 用户名
                lineData: [],
                starData: [], // 收藏本
                noteBook: [] // 生词本
            }
        })
    }

}